
const { class_controller_save,
    class_controller_find,
    class_controller_get,
    class_controller_delete, } = require("../../../controller/product/class/CLASS.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");
const pageSizeList = require("../../../utils/slicePageSize");
const { getListToTree } = require("../../../utils/class.uritls");

/**
 * @description 行业类目路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/product/class");

/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        const {        // shopId,
            id,
            parentId = undefined,
            className = undefined,
            sort = undefined,
            status = undefined
        } = ctx.request.body
        const check = emptyCheck({ className, parentId })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await class_controller_save({
                id, parentId, className, sort, status
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

/**@description 获取行业类目树列表 */
router.post("/list", async (ctx, next) => {
    try {
        const { current = 1, pageSize = 1000, className = undefined, status = undefined } = ctx.request.body
        const res = await class_controller_find({ className, status, pageSize: pageSize, current: current });
        const children = getListToTree(res.data.list, "0")
        console.log("行业类目树：", res)
        ctx.body = {
            code: 0,
            msg: "查询成功",
            data: {
                list: [
                    {
                        id: "0",
                        className: "主行业类目",
                        parentId: "xx",
                        sort: 1,
                        status: 1,
                        children: children
                    }

                ],
                total: res.data.totalCount
            }
        };
    } catch (error) {
        console.log(error);
        ctx.body = serverError
    }
})


/**@description 根据id获取行业类目详情 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await class_controller_get({ id });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除行业类目 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await class_controller_delete({ ids: [id] });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})




// // 查询列表
// router.post("/list", async (ctx, next) => {
//     try {
//         const { current, pageSize, cateName, } = ctx.request.body;
//         const res = await class_controller_find({
//             current, pageSize, cateName
//         })
//         const list = pageSizeList({ data: res, pageSize, current })
//         ctx.body = {
//             status: "0000",
//             message: "查询成功",
//             data: {
//                 total: res.length,
//                 list: list
//             }
//         }
//     } catch (error) {
//         console.log(error)
//         ctx.body = serverError
//     }

// })


// // 删除
// router.post("/delete", async (ctx, next) => {
//     try {
//         const { ids = [] } = ctx.request.body
//         if (ids.length === 0) {
//             ctx.body = {
//                 status: '0011',
//                 message: "未选择需要删除的数据"
//             }
//         }
//         else {
//             const res = await class_controller_delete({ ids: ids })
//             ctx.body = res;
//         }
//     } catch (error) {
//         ctx.body = serverError
//     }
// })



module.exports = router